/**
 * 
 */
package edu.arizona.eller.mis.liferoulette.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import edu.arizona.eller.mis.liferoulette.entity.ActivityBean;

/**
 * @author Blaine Boule - blaineb@email.arizona.edu
 *
 */
public class ActivityDAO extends BaseDAO {
	public static final String _ACTIVITY_TABLE = "ACTIVITY";
	public static final String _CREATE_STATEMENT = "CREATE TABLE " + _ACTIVITY_TABLE  +" (id INTEGER PRIMARY KEY AUTOINCREMENT, activity text not null)";
	private String[] columnArray = { ActivityBean._id, ActivityBean._activity };
	
	public ActivityDAO(Context context) {
		super(context);
	}
	
	public List<ActivityBean> getActivities() {
		List<ActivityBean> list = new ArrayList<ActivityBean>();
		Cursor cursor = db.query(_ACTIVITY_TABLE, columnArray,  null, null, null, null, ActivityBean._id + " desc");
		
		if(cursor.moveToFirst()){
			
			do{
				ActivityBean a = new ActivityBean(cursor.getInt(0), cursor.getString(1));
				list.add(a);
			}while(cursor.moveToNext());
			
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
			}
		}
		return list;
	}
	
	public ActivityBean findById(Integer id){
		ActivityBean up = null;
		Cursor cursor = db.query(_ACTIVITY_TABLE, columnArray,  ActivityBean._id + "=" + id, null, null, null, null);
		
		if(cursor.moveToFirst()){
			up = new ActivityBean(cursor.getInt(0), cursor.getString(1));
		}
		
		return up;
	}
	
	public int update(ActivityBean a){
		ContentValues update = new ContentValues();
		update.put(ActivityBean._activity, a.getActivity());
		
		return db.update(_ACTIVITY_TABLE, update, ActivityBean._id + "=" + a.getId(), null);
	}
	
	public long insert(ActivityBean a) {
	
		ContentValues insert = new ContentValues();
		insert.put(ActivityBean._activity, a.getActivity());
		
		return db.insert(_ACTIVITY_TABLE, null, insert);	
	}
	
	public int delete(ActivityBean a){
		
		return db.delete(_ACTIVITY_TABLE, ActivityBean._id + "=" + a.getId(), null);
	}
	

}
